package cn.acewill.yxd.yunxundian;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.support.v4.view.ViewCompat;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.SearchView;
import android.text.InputFilter;
import android.text.Spanned;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import org.xutils.DbManager;
import org.xutils.x;

import java.lang.reflect.Field;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import cn.acewill.yxd.yunxundian.utils.Eyes;

public class BaseActivity extends AppCompatActivity {

	protected YXDAPP yxdapp=new YXDAPP();
	protected DbManager db = x.getDb(yxdapp.getDaoConfig());

	/** 显示当前Activity的标题名称 */
	private String titleBar = "";
	private static SharedPreferences sp;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		// 在父类Activity中来绑定title布局中的内容
		setStatusBar();
		//Eyes.translucentStatusBar(this,false); 隐藏statusbar
		View titleText = findViewById(R.id.textTitle);
		View backBtn = findViewById(R.id.backBtn);
			TextView title_text = (TextView) titleText;if (titleText != null) {
			title_text.setText(titleBar);
		}
		if (backBtn != null) {
			backBtn.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					finish();// 关闭当前界面
				}
			});
		}
		/*友盟数据统计*/
	//	PushAgent.getInstance(context).onAppStart();

	}

	public void setTitleBar(String title) {
		this.titleBar = title;
	}

	/**
	 * 自定义toast
	 */
//	public void showToast(String message) {
//		Toast toast = Toast.makeText(this, message, Toast.LENGTH_SHORT);
//		View view = LayoutInflater.from(this).inflate(
//				R.layout.system_toast_view, null);
//		TextView textView = (TextView) view.findViewById(R.id.toastText);
//
//		toast.setView(view);
//		/*
//		 * 参数1：设置toast弹出框在界面中的对齐方式 参数2：X偏移量 参数3：Y偏移量
//		 */
//		toast.setGravity(Gravity.CENTER, 0, 0);
//		textView.setText(message);
//		toast.show();
//	}
	public static void showToast(Context mContext, String message) {
//		Toast toast = Toast.makeText(mContext, message, Toast.LENGTH_SHORT);
//		View view = LayoutInflater.from(mContext).inflate(
//				R.layout.system_toast_view, null);
//		TextView textView = (TextView) view.findViewById(R.id.toastText);
//
//		toast.setView(view);
//		/*
//		 * 参数1：设置toast弹出框在界面中的对齐方式 参数2：X偏移量 参数3：Y偏移量
//		 */
//		toast.setGravity(Gravity.CENTER, 0, 0);
//		textView.setText(message);
//		toast.show();
		Toast.makeText(mContext,message,Toast.LENGTH_SHORT).show();
	}

   //小米toast
	public static void showAnimaDialog(Context context,String message){
//		MyToast toast  = MyToast.makeText(context,message,MyToast.LENGTH_SHORT);
//		View view = LayoutInflater.from(context).inflate(
//				R.layout.system_toast_view, null);
//		TextView textView = (TextView) view.findViewById(R.id.toastText);
//		toast.setView(view);
//		textView.setText(message);
//		toast.setAnimations(R.style.toast_anim_view);
//		toast.show();
		Toast.makeText(context,message,Toast.LENGTH_SHORT).show();
	}

	public static String getNowDate(){
		Date date111 = new Date();
		SimpleDateFormat sdf111 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String currentDate = sdf111.format(date111); // 当期日期

		return currentDate;
	}
	public static String getMonth_Year(){
		Date date = new Date();
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM");
		String nowdate=sdf.format(date);
		return nowdate;
	}
	public static String getMonth_Year_day(){
		Date date = new Date();
		SimpleDateFormat sdf=new SimpleDateFormat("yyyyMMdd");
		String nowdate=sdf.format(date);
		return nowdate;
	}

	/**
	 * 货币单位的格式化
	 */
	private DecimalFormat df = new DecimalFormat();

	public String formatMoney(double money) {
		df.setMaximumFractionDigits(2);
		df.setMinimumFractionDigits(0);
		return df.format(money);
	}

	public String formatMoney(double money, int maximum) {
		df.setMaximumFractionDigits(maximum);
		df.setMinimumFractionDigits(0);
		return df.format(money);
	}

	public String getUrl() {
		sp = getSharedPreferences("yxdUrl", MODE_PRIVATE);
		String url = sp.getString("url", "erro");
		return url;
	}

	//判断闰年
  public static boolean isLeap(int year)
	{
		if (((year % 100 == 0) && year % 400 == 0) || ((year % 100 != 0) && year % 4 == 0))
			return true;
		else
			return false;
	}

	//返回当月天数
	public static int getDays(int year, int month)
	{
		int days;
		int FebDay = 28;
		if (isLeap(year))
			FebDay = 29;
		switch (month)
		{
			case 1:
			case 3:
			case 5:
			case 7:
			case 8:
			case 10:
			case 12:
				days = 31;
				break;
			case 4:
			case 6:
			case 9:
			case 11:
				days = 30;
				break;
			case 2:
				days = FebDay;
				break;
			default:
				days = 0;
				break;
		}
		return days;
	}

	/**
	 * 禁止EditText输入空格
	 * @param editText
	 */
	public static void setEditTextInhibitInputSpace(EditText editText){
		InputFilter filter=new InputFilter() {
			@Override
			public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
				if(source.equals(" ")) return "";
                else return null;
			}
		};
		editText.setFilters(new InputFilter[]{filter});
	}

	/**
	 * 禁止EditText输入特殊字符
	 * @param editText
	 */
	public static void setEditTextInhibitInputSpeChat(EditText editText){

		InputFilter filter=new InputFilter() {
			@Override
			public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
				String speChat="[`~!@#$%^&*()+=|{}':;',\\[\\].<>/?~！@#￥%……&*（）——+|{}【】‘；：”“’。，、？]";
				Pattern pattern = Pattern.compile(speChat);
				Matcher matcher = pattern.matcher(source.toString());
				if(matcher.find())return "";
				else return null;
			}
		};
		editText.setFilters(new InputFilter[]{filter});
	}

	public void setStatusBar(){

		//5.0使用materialDesign,此版本以下有bug
		if (Build.VERSION.SDK_INT>=Build.VERSION_CODES.LOLLIPOP){
			Window window = getWindow();
//取消设置透明状态栏,使 ContentView 内容不再覆盖状态栏
			window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);

//需要设置这个 flag 才能调用 setStatusBarColor 来设置状态栏颜色
			window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
//设置状态栏颜色
			window.setStatusBarColor(ContextCompat.getColor(this,R.color.blue));

			ViewGroup mContentView = (ViewGroup) findViewById(Window.ID_ANDROID_CONTENT);
			View mChildView = mContentView.getChildAt(0);
			if (mChildView != null) {
				//注意不是设置 ContentView 的 FitsSystemWindows, 而是设置 ContentView 的第一个子 View . 预留出系统 View 的空间.
				ViewCompat.setFitsSystemWindows(mChildView, true);
			}
		}
	}

	//去掉下划线searchview
	public static void disPlayUnderLine(SearchView searchView){
		if (searchView != null) {
			try {        //--拿到字节码
				Class<?> argClass = searchView.getClass();
				//--指定某个私有属性,mSearchPlate是搜索框父布局的名字
				Field ownField = argClass.getDeclaredField("mSearchPlate");
				//--暴力反射,只有暴力反射才能拿到私有属性
				ownField.setAccessible(true);
				View mView = (View) ownField.get(searchView);
				//--设置背景
				mView.setBackgroundResource(R.drawable.ev_search_selector);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}

	}


}
